package com.liuchuanzheng.lcz_changeconfig

import android.os.Bundle
import android.os.Handler
import android.os.Process
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.liuchuanzheng.lcz_changeconfig.config.Config
import com.liuchuanzheng.lcz_changeconfig.config.ConfigManager
import com.liuchuanzheng.lcz_changeconfig.databinding.ActivityChangeConfigBinding

/**
 * @author 刘传政
 * @date 3/4/21 3:36 PM
 * QQ:1052374416
 * 电话:18501231486
 * 作用:
 * 注意事项:
 */
class ChangeConfigActivity : AppCompatActivity() {
    lateinit var mViewBinding: ActivityChangeConfigBinding
    val configList: MutableList<Config> = mutableListOf()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mViewBinding = ActivityChangeConfigBinding.inflate(layoutInflater)
        setContentView(mViewBinding.root)
        mViewBinding.recyclerview.layoutManager = LinearLayoutManager(this)
        val configAdapter = ConfigAdapter(configList)
        mViewBinding.recyclerview.adapter = configAdapter
        configAdapter.setOnItemClickListener(object : ConfigAdapter.OnItemClickListener {
            override fun onItemClick(view: View, position: Int) {
                if (ConfigManager.getCurrentConfig().mName.equals(configList.get(position).mName)) {
                    //就是当前环境,那就没必要切换了
                    Toast.makeText(this@ChangeConfigActivity, "当前使用的就是所选环境,没必要切换", Toast.LENGTH_LONG).show()
                    return
                }else{
                    ConfigManager.changeCurrentConfig(configList.get(position).mName)
                    // TODO: 3/4/21 正式使用时要清除登录相关缓存信息


                    //退出app要进行退出登录和去除数据相关

                    Toast.makeText(this@ChangeConfigActivity, "2s后将自动关闭App，重启生效", Toast.LENGTH_LONG).show()
                    Handler().postDelayed({
                        Process.killProcess(Process.myPid())
                    }, 2000)
                }

            }

        })
        val allConfigs = ConfigManager.getAllConfigs()
        configList.clear()
        configList.addAll(allConfigs)
        configAdapter.notifyDataSetChanged()

        val currentConfig = ConfigManager.getCurrentConfig()
        val stringBuilder = StringBuilder()
        stringBuilder.append("当前使用的环境是:\n")
        stringBuilder.append("名称: ${currentConfig.mName}\n")
        stringBuilder.append("tips: ${currentConfig.tips}\n")
        stringBuilder.append("url: ${currentConfig.url}")
        mViewBinding.tvTips.text = stringBuilder.toString()

    }

    class ConfigAdapter(private val configList: List<Config>) :
        RecyclerView.Adapter<ConfigAdapter.ViewHolder>() {
        private var mOnItemClickListener: OnItemClickListener? = null

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(
                R.layout.item_config,
                parent,
                false
            )
            return ViewHolder(view)
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val entity = configList[position]

            try {
                val stringBuilder = StringBuilder()
                stringBuilder.append("名称: ${entity.mName}\n")
                stringBuilder.append("tips: ${entity.tips}\n")
                stringBuilder.append("url: ${entity.url}")
                holder.tvTips.text = stringBuilder.toString()

            } catch (e: Exception) {
                e.printStackTrace()
            }

            //判断是否设置了监听器
            if (mOnItemClickListener != null) {
                //为ItemView设置监听器-
                holder.itemView.setOnClickListener {
                    val position = holder.layoutPosition // 1
                    mOnItemClickListener!!.onItemClick(holder.itemView, position) // 2
                }

            }
        }

        override fun getItemCount(): Int {
            return configList.size
        }

        inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

            var tvTips = itemView.findViewById(R.id.tvTips) as TextView

        }


        interface OnItemClickListener {
            fun onItemClick(view: View, position: Int)
        }

        fun setOnItemClickListener(onItemClickListener: OnItemClickListener) {
            this.mOnItemClickListener = onItemClickListener
        }
    }

}